const fs = require("fs");
const chalk = require("chalk");
const parse = require ('parse-git-config') ;

chalk.level = 2;

let space4 = `| `;
let addSpace = ``;
let fileSaves = [];


const  trim =(x) => {
    return x.replace(/^\s+|\s+$/gm,'');
  }
const diguiFolder = (currentFolder, counter) => {
    // console.log(chalk.red(currentFolder));
    // console.log(chalk.blue(currentFolder), 'currentFolder');

    let block = ``;
    if (fs.existsSync(currentFolder)) {
        let data = fs.statSync(currentFolder);
        for(let i = 0; i<counter; i++) {
            block += `${space4}`;
        }

        block = trim(block);

        counter++;
        if (data.isDirectory()) {

          
            let files = fs.readdirSync(currentFolder);
            addSpace = `${addSpace}` + `${space4}`

            
            
            files.forEach(item => {
                if (fs.statSync(`${currentFolder}/${item}`).isDirectory()) {

                 
                    if (item === '.git') {
                      

               

                        if (parse.sync()['remote "origin"'] == undefined) {
                            return 
                        }

                        console.log(`git地址:${chalk.white("(")}${chalk.redBright(parse.sync()['remote "origin"'].url)}${chalk.white(")")}`);
                        console.log(`${chalk.white(".")}`)
                    }
                    if (item === 'node_modules' || item === '.git' || item === '.next') return ;
                    console.log(`${chalk.whiteBright(block)}${chalk.whiteBright('____')}${chalk.blueBright(item)}`);
                    diguiFolder(`${currentFolder}/${item}`, counter);
                } else {
                    console.log(`${chalk.whiteBright(block)}${chalk.whiteBright('____')}${chalk.whiteBright(item)}`);
                }
                // console.log(`${addSpace}${item}`);
            })
            
        }

    } else {
        //路径不存在
        console.log(2);
    }
}

module.exports = {
    diguiFolder
}